<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" label-width="120px">
	<el-form-item prop="postCode" :label="$t('post.postCode')">
		<el-input v-model="dataForm.postCode" :placeholder="$t('post.postCode')"></el-input>
	</el-form-item>
	<el-form-item prop="postName" :label="$t('post.postName')">
		<el-input v-model="dataForm.postName" :placeholder="$t('post.postName')"></el-input>
	</el-form-item>
	<el-form-item prop="postRank" :label="$t('post.postRank')">
		<el-input-number size="small" v-model="dataForm.postRank" controls-position="right" :min="0" :label="$t('menu.sort')"></el-input-number>
	</el-form-item>
	<el-form-item prop="status" :label="$t('status')" size="mini">
	  <el-radio-group v-model="dataForm.status">
	    <el-radio :label="1">{{ $t('status1') }}</el-radio>
		<el-radio :label="0">{{ $t('status0') }}</el-radio>
	  </el-radio-group>
	</el-form-item>
	<el-form-item prop="postType" :label="$t('post.postType')">
		<el-input v-model="dataForm.postType" :placeholder="$t('post.postType')"></el-input>
	</el-form-item>
	<el-form-item prop="remark" :label="$t('remark')">
	  <el-input v-model="dataForm.remark" :placeholder="$t('remark')"></el-input>
	</el-form-item>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import { isEmail, isMobile } from '@/utils/validate'
export default {
  data () {
    return {
      visible: false,
	  modifyExtendsParam: {},
	  extendsParam: {},
      dataForm: {
        id: '',
		postId: '',
        postCode: '',
        postName: '',
        postRank: 1,
        remark: '',
        status: 1,
        postType: ''
      }
    }
  },
  computed: {
    dataRule () {
		 return {
		   postName: [
			 { required: true, message: this.$t('validate.required'), trigger: 'blur' }
		   ],
		   postCode: [
			 { required: true, message: this.$t('validate.required'), trigger: 'change' }
		   ]
		 }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields();
		this.dataForm.postId = this.modifyExtendsParam.postId;
		if(this.dataForm.postId){
			this.dataForm = {
				... this.dataForm,
				... this.modifyExtendsParam
			}
		}
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
		var url = !this.dataForm.postId ? '/pc/post/add' : '/pc/post/modify'
        this.$http.post(url, this.dataForm).then(({ data: res }) => {
          if (res.code !== 200) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>

<style lang="scss">
.mod-sys__user {
  .role-list {
    .el-select {
      width: 100%;
    }
  }
}
</style>
